package com.hehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hehe.powernode.mapper.SysUserMapper;
import com.hehe.powernode.model.Result;
import com.hehe.powernode.pojo.SysUser;
import com.hehe.service.SysUserService;
import com.hehe.util.AuthUtils;
import com.hehe.vo.SysUserAddOrUpdateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统用户接口管理")
@RestController
@RequestMapping("/sys/user")
@Slf4j
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 根据用户ID查询用户信息
     * 请求地址/sys/user/info
     * 根据用户ID查询用户信息
     *
     * @return
     */
    @ApiOperation("根据用户ID查询用户信息")
    @GetMapping("/info")
//    @PreAuthorizeuthorize("hasAnyAuthority('?')")
    public Result<SysUser> userResult() {
        Long loginUserId = AuthUtils.getLoginUserId();
        SysUser sysUser = sysUserService.getById(loginUserId);
        return Result.success(sysUser);
    }

    /**
     * 管理员列表
     *
     * @return
     */
    @ApiOperation("管理员列表")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:user:page')")
    public Result<?> managerPage(@RequestParam int current, @RequestParam int size,
                       @RequestParam(required = false) String username) {
        Page<SysUser> sysUserPage = new Page<>(current, size);
        sysUserPage = sysUserService.page(sysUserPage, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(username), SysUser::getUsername, username));
        return Result.success(sysUserPage);
    }

    /**
     * 批量删除管理员
     * @return
     */
    @ApiOperation("批量删除管理员")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAnyAuthority('sys:user:delete')")
    public Result<String> removeSysUsers(@PathVariable List<Long> userIds) {
        //---/sys/user/1, 4, 7, 8, 9, 10, 11, 12
        log.info("removeSysUsers userIds:{}",userIds);
        //---userIds:[1, 4, 7, 8, 9, 10, 11, 12]
        return Result.handle(false);
    }
    /**
     * 添加新用户
     * @return
     */
    @ApiOperation("添加新用户")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:user:add')")
    public Result<?> addUser(@RequestBody SysUserAddOrUpdateParam sysUserAddOrUpdateParam) {
        log.info("addUser sysUserAddOrUpdateParam:{}",sysUserAddOrUpdateParam);
        return Result.handle(false);
    }

}
